package org.i_plus.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.i_plus.Assistance;
import org.i_plus.model.HobbyBean;
import org.i_plus.model.MemberBean;
import org.i_plus.model.services.MemberService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;



@WebServlet("/MemberServlet2")
@MultipartConfig
public class MemberServlet2 extends HttpServlet {
	private MemberService service;
	public void init() throws ServletException {
		WebApplicationContext context =
				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		this.service = (MemberService) context.getBean("MemberService");		
	}
	@Override
	protected void doGet(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		
		InputStream temp3 = request.getPart("image_file").getInputStream();
		String x1 = request.getParameter("x1");
		String y1 = request.getParameter("y1");
		String w = request.getParameter("w");
		String h = request.getParameter("h");
		
		Map<String,String> errors2=new HashMap<String,String>();
		request.setAttribute("errors2", errors2);
		if(request.getPart("image_file").getSize() != 0){
			String contentType = request.getPart("image_file").getContentType();
			int checkimg = contentType.indexOf("image");
			if(checkimg == -1){
				errors2.put("pic", "上傳檔案錯誤，請上傳圖檔");
			}
		}
		
		byte[] image_file1 = new byte[(int)request.getPart("image_file").getSize()];
		temp3.read(image_file1);
		image_file1 = Assistance.cropper(image_file1, Integer.parseInt(y1), Integer.parseInt(x1), Integer.parseInt(w),Integer.parseInt(h));
		
		String pro=request.getParameter("inp");

		HttpSession ses = request.getSession();
 		MemberBean bean=(MemberBean)ses.getAttribute("presendMember");
 		HobbyBean hbean=(HobbyBean)ses.getAttribute("presendHobby");
 		bean.setPic1(image_file1);
 		
 		if(errors2!=null && !errors2.isEmpty()){
			request.getRequestDispatcher("member2.jsp").
			forward(request, response);
			return;
		}
 		
		//session.setAttribute("user",bean.getAccount());
 		//session.setAttribute("userpic",bean.getpic());記得轉換
 		//session.remove("");註冊的資料

     	if(pro.equals("send"))
     	{  
     		service.insertMember(bean);
     		service.insertHobby(hbean);
    		ses.setAttribute("user", bean);
    		 if(bean.getSex().equals("m")){ 
                 request.setAttribute("bg", "男"); 
             }else{ 
                 request.setAttribute("bg", "女"); 
             } 
             //用登入後的account去抓"興趣"的bean！ 
             Assistance ass=new Assistance();//new 這個class 
             String blood=ass.howTypeBlood(bean.getBlood());//呼叫方法，判斷血型！ 
             request.setAttribute("blood", blood); 
             int old=ass.howOldAreYou(bean.getBdate());//呼叫方法！這方法是判斷幾歲！ 
             request.setAttribute("old",old); 
             String star=ass.howSignAreYou(bean.getBdate());//呼叫方法！這方法是判斷什麼星座！ 
             request.setAttribute("starSign", star); 
             String hobbys=ass.hobbyTrans(hbean); 
             request.setAttribute("hobby", hobbys); 
    		
    		if(bean.getPic1()!=null)
    		{
    		ses.setAttribute("userpic",Assistance.photoByteToImgString("image/jpeg", bean.getPic1()));
    		request.setAttribute("temppicture1", Assistance.photoByteToImgString("image/jpeg", bean.getPic1()));
    		}else{
    		ses.setAttribute("userpic",null);
    		request.setAttribute("temppicture1",null);
    		}
    		ses.removeAttribute("presendMember");
    		ses.removeAttribute("presendHobby");
    		//request.getRequestDispatcher("/View/MypageServlet.controller?account="+bean.getAccount()).
    		request.getRequestDispatcher("/index.jsp").
    		forward(request, response);
     	}
    
     }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}
